const app = getApp();

Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    ids: "",
    allList: null,
    price: 0.0,
    userPass: "",
    isFlag: false
  },
  lifetimes: {
    attached() {
      const userId = app.globalData.userId;
      wx.request({
        url: `${app.globalData.local_url}api/myShopCar`,
        data: { userId },
        method: 'GET',
        header: { 'content-type': 'application/json' },
        success: (res) => {
          if (res.data.code === 200) {
            this.setData({ allList: res.data.data });
          }
        }
      });
      // 检查购物车是否为空
      if (!this.data.allList || this.data.allList.length === 0) {
        // 如果为空，则设置一个标志来显示没有商品的视图
        this.setData({ showNoGoods: true });
      }
    }
  },
  methods: {
    delete() {
      const ids = this.data.allList
        .filter(item => item.check)
        .map(item => item.cartId)
        .join(',');

      if (!ids) {
        wx.showToast({
          title: "请选择删除的游戏",
          icon: 'none',
          duration: 2000
        });
        return;
      }

      const userId = app.globalData.userId;
      wx.request({
        url: `${app.globalData.local_url}api/deleteCart`,
        data: { ids },
        method: 'POST',
        header: { 'content-type': 'application/json' },
        success: (res) => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            });
            this.setData({ modalName: null });
            this.refreshCart(userId);
          }
        }
      });
    },
    refreshCart(userId) {
      wx.request({
        url: `${app.globalData.local_url}api/myShopCar`,
        data: { userId },
        method: 'GET',
        header: { 'content-type': 'application/json' },
        success: (res) => {
          if (res.data.code === 200) {
            this.setData({ allList: res.data.data });
          }
        }
      });
    },
    quanxuan() {
      const isFlag = !this.data.isFlag;
      const allList = this.data.allList.map(item => ({
        ...item,
        check: isFlag
      }));
      const price = isFlag ? allList.reduce((sum, item) => sum + item.price, 0) : 0;

      this.setData({ isFlag, allList, price });
    },
    danxuan(e) {
      const index = e.currentTarget.dataset.index;
      const allList = [...this.data.allList];
      allList[index].check = !allList[index].check;
      const price = allList.reduce((sum, item) => item.check ? sum + item.price : sum, 0);

      this.setData({ allList, price });
    },
    userPass(e) {
      this.setData({ userPass: e.detail.value });
    },
    showModal(e) {
      const ids = this.data.allList
        .filter(item => item.check)
        .map(item => item.cartId)
        .join(',');

      if (!ids) {
        wx.showToast({
          title: "请选择结算的游戏",
          icon: 'none',
          duration: 2000
        });
        return;
      }

      this.setData({
        modalName: e.currentTarget.dataset.target,
        ids
      });
    },
    hideModal() {
      this.setData({ modalName: null });
    },
    pay() {
      if (this.data.userPass !== '123456') {
        wx.showToast({
          title: "支付密码不正确",
          icon: 'none',
          duration: 2000
        });
        return;
      }

      const userId = app.globalData.userId;
      wx.request({
        url: `${app.globalData.local_url}api/addOrder`,
        data: { ids: this.data.ids, userId },
        method: 'POST',
        header: { 'content-type': 'application/json' },
        success: (res) => {
          if (res.data.code === 200) {
            wx.showToast({
              title: '订单支付成功',
              icon: 'success',
              duration: 2000
            });
            this.setData({ modalName: null });
            this.refreshCart(userId);
          }
        }
      });
    }
  }
});
